 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection myConnection = new SqlConnection("user id=sa;" +
                                       "password=########;server=194.0.9.75\\Sqlserver;" +
                                       "Trusted_Connection=yes;" +
                                       "database=DB; " +
                                       "connection timeout=30");
            SqlDataReader myReader = null;
            try
            {
                myConnection.Open();
                Console.WriteLine("成功！");
                String sqlstr = "SELECT TOP 10 s_mark,s_office FROM Public_office where s_first_no='07' and s_fourth_no='00' and s_third_no<>'00' and s_office<>'护理组' and s_sort=1";
                SqlCommand myCommand = new SqlCommand(sqlstr, myConnection); // - or - myCommand.Connection = myConnection;
                myReader = myCommand.ExecuteReader();
                while (myReader.Read())
                {
                    Console.WriteLine(myReader["s_mark"].ToString());
                    Console.WriteLine(myReader["s_office"].ToString());
                }

                /*
                 * 插入/更新
                SqlCommand myCommand = new SqlCommand("INSERT INTO table (Column1, Column2) " +
                                     "Values ('string', 1)", myConnection);
                // - or - myCommand.CommandText = "INSERT INTO table (Column1, Column2) " + "Values ('string', 1)";

                myCommand.ExecuteNonQuery();


                *参数绑定
                SqlParameter myParam = new SqlParameter("@Param1", SqlDbType.VarChar, 11);
                myParam.Value = "Garden Hose";
        
                SqlParameter myParam2 = new SqlParameter("@Param2", SqlDbType.Int, 4);
                myParam2.Value = 42;

                SqlParameter myParam3 = new SqlParameter("@Param3", SqlDbType.Text);
                myParam.Value = "Note that I am not specifying size. " + 
                  "If I did that it would trunicate the text.";
                 
                SqlCommand myCommand = new SqlCommand(
                  "SELECT * FROM table WHERE Column = @Param2", myConnection);
                myCommand.Parameters.Add(myParam2);
                 * 
                 * 
                 */
            }
            catch(Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            
        }
    }
}
